package ICM.client;

import java.io.IOException;
import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import common.CommandMessage;
import common.Request;
import common.TextMessage;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class RequestsInfo extends JPanel implements Observer {
	
	private ClientUI ui;
	private Client c;
	private Request[] requests = null;
	private JTable table;
	private JScrollPane pane;
	private DefaultTableModel defaultModel = null;
	private Vector newRow = null;
	private String StageName = null;
	private JButton btnBack;
	private JLabel lblEmployeesInfo;
	private JLabel lblRequest;
	private JLabel lblDepartment;
	private JLabel lblUserId;
	private JLabel lblRole;
	private JLabel lblEmail;
	private JLabel lblRequestDate;
	private JLabel lblDescription;
	private JLabel lblComments;
	private JLabel lblReasons;
	private JTextPane Description;
	private JTextPane Comments;
	private JTextPane requestID;
	private JTextPane Department;
	private JTextPane UserID;
	private JTextPane Role;
	private JTextPane Email;
	private JTextPane RequestDate;
	private JTextPane Reasons;
	private JLabel lblStatus;
	private JTextPane Status;
	private JLabel lblCurrentStage;
	private JTextPane Stage;
	private JTextPane DateOfStartTreatment;
	private JLabel lblDateOfStart;
	private JLabel lblSupervisor;
	private JTextPane Supervisor;
	
	public RequestsInfo(final Client c, final ClientUI ui)
	{
		this.c = c;
		this.ui = ui;
		c.addObserver(this);
		setLayout(null);
		
		CommandMessage msg = new CommandMessage("Get requests",null);
		try {
			c.sendToServer(msg);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
		    Thread.sleep(400);
		} catch(InterruptedException ex) {
		    Thread.currentThread().interrupt();
		}
		
		int i;
		table = new JTable();
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				JTable target = (JTable)e.getSource();
				int row = target.getSelectedRow();
				
				if (!lblRequest.isVisible())
				{
					lblRequest.setVisible(true);
					lblDepartment.setVisible(true);
					lblUserId.setVisible(true);
					lblRole.setVisible(true);
					lblEmail.setVisible(true);
					lblRequestDate.setVisible(true);
					lblDescription.setVisible(true);
					lblComments.setVisible(true);
					requestID.setVisible(true);
					Department.setVisible(true);
					UserID.setVisible(true);
					Role.setVisible(true);
					Email.setVisible(true);
					RequestDate.setVisible(true);
					Description.setVisible(true);
					Comments.setVisible(true);
					lblStatus.setVisible(true);
					lblCurrentStage.setVisible(true);
					lblDateOfStart.setVisible(true);
					Status.setVisible(true);
					Stage.setVisible(true);
					lblSupervisor.setVisible(true);
					Supervisor.setVisible(true);
					DateOfStartTreatment.setVisible(true);
					lblReasons.setVisible(true);
					Reasons.setVisible(true);
				}
				requestID.setText(table.getValueAt(row, 0).toString());
				Department.setText(table.getValueAt(row, 1).toString());
				UserID.setText(table.getValueAt(row, 2).toString());
				Role.setText(table.getValueAt(row, 3).toString());
				Email.setText(table.getValueAt(row, 4).toString());
				RequestDate.setText(table.getValueAt(row, 5).toString());
				Description.setText(table.getValueAt(row, 6).toString());
				Comments.setText(table.getValueAt(row, 7).toString());
				if (requests[row].getidSupervisor()!=0)
					Supervisor.setText(table.getValueAt(row, 8).toString());
				else
					Supervisor.setText("");
				if (requests[row].getDateOfStartTreatment()!=null)
					DateOfStartTreatment.setText(table.getValueAt(row, 9).toString());
				else
					DateOfStartTreatment.setText("");
				Status.setText(table.getValueAt(row, 10).toString());
				if (requests[row].getidStage()!=0)
					Stage.setText(table.getValueAt(row, 11).toString());
				else
					Stage.setText("");
				Reasons.setText(table.getValueAt(row, 12).toString());
			}
		});
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
					"Request ID", "Department", "User ID", "Role", "Email", "Request date", "Description", "Comments", "Supervisor ID", "Date of Start Treatment", "Status", "Current Stage", "Reasons"
			}
		));
		defaultModel = (DefaultTableModel) table.getModel();
        if (requests!=null)
		{
			for (i=0;i<requests.length;i++)
			{
				if (requests[i].getidStage()!=0)
				{
					// Get stage name
					CommandMessage msg11 = new CommandMessage("Get stage name",new Integer[] {requests[i].getidStage()});
					try {
						c.sendToServer(msg11);
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					try {
					    Thread.sleep(200);
					} catch(InterruptedException ex) {
					    Thread.currentThread().interrupt();
					}
				}
				newRow = new Vector();
				newRow.add(requests[i].getIdRequest());
		        newRow.add(requests[i].getDepartment());
		        newRow.add(requests[i].getUserID());
		        newRow.add(requests[i].getRole());
		        newRow.add(requests[i].getEmail());
		        newRow.add(requests[i].getdateRequest());
		        newRow.add(requests[i].getDescription());
		        newRow.add(requests[i].getComments());
		        if (requests[i].getidSupervisor()!=0)
		        	newRow.add(requests[i].getidSupervisor());
		        else
		        	newRow.add("");
		        newRow.add(requests[i].getDateOfStartTreatment());
		        newRow.add(requests[i].getStatus());
		        if (requests[i].getidStage()!=0)
		        	newRow.add(StageName);
		        else
		        	newRow.add("");
		        newRow.add(requests[i].getReasons());
		        defaultModel.addRow(newRow); 
		        StageName = null;
			}
		}
		pane = new JScrollPane(table); 
		pane.setBounds(31, 72, 867, 226);
		add(pane);
		
		lblEmployeesInfo = new JLabel("Requests info");
		lblEmployeesInfo.setFont(new Font("Tahoma", Font.PLAIN, 26));
		lblEmployeesInfo.setBounds(204, 11, 218, 37);
		add(lblEmployeesInfo);
		
		btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					ui.SetContent(ClientUI.Report_Screen);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} 
			}
		});
		btnBack.setIcon(new ImageIcon(RequestsInfo.class.getResource("/client/images/back.png")));
		btnBack.setBounds(818, 452, 114, 37);
		add(btnBack);
		
		lblRequest = new JLabel("Request #:");
		lblRequest.setVisible(false);
		lblRequest.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblRequest.setBounds(32, 350, 80, 17);
		add(lblRequest);
		
		lblDepartment = new JLabel("Department:");
		lblDepartment.setVisible(false);
		lblDepartment.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblDepartment.setBounds(32, 382, 80, 17);
		add(lblDepartment);
		
		lblUserId = new JLabel("User ID:");
		lblUserId.setVisible(false);
		lblUserId.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblUserId.setBounds(32, 413, 80, 17);
		add(lblUserId);
		
		lblRole = new JLabel("Role:");
		lblRole.setVisible(false);
		lblRole.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblRole.setBounds(32, 444, 80, 17);
		add(lblRole);
		
		lblEmail = new JLabel("Email:");
		lblEmail.setVisible(false);
		lblEmail.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblEmail.setBounds(32, 472, 80, 17);
		add(lblEmail);
		
		lblRequestDate = new JLabel("Request date:");
		lblRequestDate.setVisible(false);
		lblRequestDate.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblRequestDate.setBounds(32, 505, 94, 17);
		add(lblRequestDate);
		
		lblDescription = new JLabel("Description:");
		lblDescription.setVisible(false);
		lblDescription.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblDescription.setBounds(387, 379, 80, 17);
		add(lblDescription);
		
		lblComments = new JLabel("Comments:");
		lblComments.setVisible(false);
		lblComments.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblComments.setBounds(387, 472, 80, 17);
		add(lblComments);
		
		Description = new JTextPane();
		Description.setEditable(false);
		Description.setVisible(false);
		Description.setBounds(477, 350, 276, 65);
		add(Description);
		
		Comments = new JTextPane();
		Comments.setEditable(false);
		Comments.setVisible(false);
		Comments.setBounds(477, 444, 276, 65);
		add(Comments);
		
		requestID = new JTextPane();
		requestID.setEditable(false);
		requestID.setVisible(false);
		requestID.setBounds(120, 350, 208, 20);
		add(requestID);
		
		Department = new JTextPane();
		Department.setEditable(false);
		Department.setVisible(false);
		Department.setBounds(120, 379, 208, 20);
		add(Department);
		
		UserID = new JTextPane();
		UserID.setEditable(false);
		UserID.setVisible(false);
		UserID.setBounds(120, 413, 208, 20);
		add(UserID);
		
		Role = new JTextPane();
		Role.setEditable(false);
		Role.setVisible(false);
		Role.setBounds(120, 444, 208, 20);
		add(Role);
		
		Email = new JTextPane();
		Email.setEditable(false);
		Email.setVisible(false);
		Email.setBounds(120, 472, 208, 20);
		add(Email);
		
		RequestDate = new JTextPane();
		RequestDate.setEditable(false);
		RequestDate.setVisible(false);
		RequestDate.setBounds(120, 505, 208, 20);
		add(RequestDate);
		
		lblStatus = new JLabel("Status:");
		lblStatus.setVisible(false);
		lblStatus.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblStatus.setBounds(36, 571, 57, 17);
		add(lblStatus);
		
		Status = new JTextPane();
		Status.setEditable(false);
		Status.setVisible(false);
		Status.setBounds(181, 568, 147, 20);
		add(Status);
		
		lblCurrentStage = new JLabel("Current Stage:");
		lblCurrentStage.setVisible(false);
		lblCurrentStage.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblCurrentStage.setBounds(35, 599, 94, 17);
		add(lblCurrentStage);
		
		Stage = new JTextPane();
		Stage.setEditable(false);
		Stage.setVisible(false);
		Stage.setBounds(181, 596, 147, 20);
		add(Stage);
		
		DateOfStartTreatment = new JTextPane();
		DateOfStartTreatment.setEditable(false);
		DateOfStartTreatment.setVisible(false);
		DateOfStartTreatment.setBounds(181, 624, 147, 20);
		add(DateOfStartTreatment);
		
		lblDateOfStart = new JLabel("Date of start treatment:");
		lblDateOfStart.setVisible(false);
		lblDateOfStart.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblDateOfStart.setBounds(35, 627, 160, 17);
		add(lblDateOfStart);
		
		lblSupervisor = new JLabel("Supervisor:");
		lblSupervisor.setVisible(false);
		lblSupervisor.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblSupervisor.setBounds(31, 533, 80, 17);
		add(lblSupervisor);
		
		lblReasons = new JLabel("Reasons:");
		lblReasons.setVisible(false);
		lblReasons.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblReasons.setBounds(393, 571, 74, 17);
		add(lblReasons);
		
		Reasons = new JTextPane();
		Reasons.setVisible(false);
		Reasons.setBounds(477, 545, 282, 65);
		add(Reasons);
		
		Supervisor = new JTextPane();
		Supervisor.setVisible(false);
		Supervisor.setBounds(120, 533, 208, 22);
		add(Supervisor);
	}
	
	/**
	 * Handle massage from Observable class in our case Client
	 */
	@Override
	public void update(Observable o, Object arg1) {
		// TODO Auto-generated method stub
		String msg = "";
		String cmd = "";
		if (arg1 instanceof TextMessage)
			msg = ((TextMessage)arg1).ToDebugMessage();
		else if (arg1 instanceof CommandMessage) {
			CommandMessage ms=(CommandMessage)arg1;
			if(ms.getCommand().equals("All requests"))
					requests = (Request[]) ms.getParams()[1];
			else if(ms.getCommand().equals("Stage name"))
				StageName = (String) ms.getParams()[1];
		}
		else
			msg = arg1.toString();
	}
}
